'use strict'

global.ENV=process.env.NODE_ENV;

let {v4}=require('uuid');
const {Pool} = require('pg');
let config = require('../env.js');

console.log(config);
let pool = new Pool(config);
let query = async () => {
  let connect = await pool.connect();

  try {
    // 有表删表，没表创表
    await connect.query('drop table if exists contents');

    await connect.query(`create table if not exists contents
    (
      title       varchar(255),
      content     text,
      posted      boolean default true,
      deleted     boolean default false,
      id          uuid         not null
        constraint contents_id_pk
        primary key,
      module_name varchar(255) not null
    )`);

    await connect.query(`create unique index contents_id_uindex on contents (id) `);

    if(global.ENV==='dev') {
      let id=v4();

      await connect.query(`insert into contents (id,title,module_name,content,posted) values
      ($1::uuid,$2::varchar,$3::varchar,$4::text,$5::boolean)`,[id,'如何批量导入员工？','employee','test',true])
    }

  } finally {
    connect.release()
  }
};

query().catch(e => console.error(e.message, e.stack));
